package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.CareOrderDao;
import com.aaa.doctor.dao.CareOrderItemDao;
import com.aaa.doctor.feign.CheckFeign;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.CareOrderItemService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.doctor.vo.CareOrderVo;
import com.aaa.entity.*;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.8.0_181
 * @Author ZXJ
 * @Date 2023/3/25 15:03
 * @注释
 */
@Service
public class CareOrderItemServiceImpl implements CareOrderItemService {
    @Resource
    private CareOrderItemDao careOrderItemDao;
    @Override
    public Result<List<CareOrderItem>> getAllChuFang(String patientId) {
        List<CareOrderItem> allChuFang = careOrderItemDao.getAllChuFang(patientId);
        return new Result<>(200,"患者信息处方查询成功",allChuFang);
    }

    @Resource
    private CareOrderDao careOrderDao;

    @Resource
    private UserFeign userFeign;
    @Override//保存药品处方
    public Result<String> saveCareOrderItem(CareOrderVo careOrderVo) {
        //获取药
        CareOrder careOrder=careOrderVo.getCareOrder();
        //检查
        List<CareOrderItem> careOrderItems=careOrderVo.getCareOrderItems();
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();//用户id
        String userName = userFeign.getByUsername(phone).getUserName();
        IdWorker worker = new IdWorker();
        long l = worker.nextId();
        String coId ="CO"+ l;//生成coId 存到his_care_order表中
        careOrder.setCoId(coId);
        careOrder.setUserId(userId);
        LocalDateTime now = LocalDateTime.now();
        careOrder.setCreateTime(now);
        careOrder.setCreateBy(userName);
        careOrderDao.insert(careOrder);
        for (CareOrderItem careOrderItem:careOrderItems){
            IdWorker worker2 = new IdWorker();
            long l2 = worker2.nextId();
            String ItemId ="ITEM"+ l2;
            careOrderItem.setItemId(ItemId);
            careOrderItem.setCoId(coId);
            careOrderItem.setStatus("0");
            careOrderItem.setCreateTime(LocalDateTime.now());
            careOrderItemDao.insert(careOrderItem);
        }
        return new Result<>(200,"成功");
    }


    @Override
    public Result<List<CareOrder>> queryCareOrdersByChId(String chId) {
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("ch_id",chId);
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper);
        for (CareOrder careOrde: careOrders) {
            QueryWrapper<CareOrderItem> wrapper1=new QueryWrapper<>();
                wrapper1.eq("co_id",careOrde.getCoId());
            List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper1);
            careOrde.setCareOrderItemList(careOrderItems);
            }
        return new Result<List<CareOrder>>(200,"药品处方结果查询成功",careOrders);

    }

    @Override//删除药品及检查详情
    public Result<String> deleteCareOrderItemById(String itemId, BigDecimal amount) {
        QueryWrapper<CareOrderItem> wrapper=new QueryWrapper<>();
        wrapper.eq("item_id",itemId);
        CareOrderItem careOrderItem = careOrderItemDao.selectOne(wrapper);
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("co_id",careOrderItem.getCoId());
        CareOrder careOrder = careOrderDao.selectOne(wrapper1);
        careOrder.setAllAmount(careOrder.getAllAmount().subtract(amount));//减去删除的金额
        careOrderDao.updateById(careOrder);
        if (careOrder.getAllAmount().intValue() == 0){
            careOrderDao.delete(wrapper1);
        }
        careOrderItemDao.delete(wrapper);
        return new Result<>(200,"删除药品及检查详情成功");
    }
}
